<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    @register="register"
    @ok="handleSubmit"
    :okAuth="'m8:order:flow:over:edit'"
    width="40%"
  >
    <template #title>
      <!-- <Icon :icon="getTitle.icon" class="pr-1 m-1" /> -->
      <span>
        完工登记 【{{
          getTitle.title
        }}】
      </span>
    </template>
    <BasicForm @register="registerForm" />
    <BasicModal
      @register="register2"
      v-bind="$attrs"
      @ok="handleOk"
      :canFullscreen="false"
      cancelText="否"
      okText="是"
      width="40%"
      :showCancelBtn="false"
    >
      <div
        style="text-align: center; height: 200px; line-height: 200px; font-size: 60px; color: red"
      >
        是否全部完成
      </div>
      <template #centerFooter>
        <a-button :loading="loading" @click="closeFunc()"> <Icon icon="ant-design:close-outlined" /> 否 </a-button>
      </template>
    </BasicModal>
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'WorkCalendarModal',
  });
</script>
<script setup lang="ts">
  import { defineComponent, ref,computed } from 'vue';
  import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { officeTreeData } from '/@/api/sys/office';
  import { overForm,M8OrderPicFlowOver } from '/@/api/m8/flow/flow';

  import { overSave } from '/@/api/m8/order/order';
  const emit = defineEmits(['success', 'register']);
  const { showMessage } = useMessage();
  // import { DatePicker } from 'ant-design-vue';
  const record = ref<M8OrderPicFlowOver>({} as M8OrderPicFlowOver);
  const getTitle = computed(() => ({
    title: '',
  }));
  let loading = ref(false);
  const [register2, { openModal, setModalProps: setModalProps2, closeModal: closeModal2 }] =
    useModal();

  // let obj= ref<any>()
  const [register, { closeDrawer, setDrawerProps }] = useDrawerInner(async (data) => {
   

    setDrawerProps({ loading: true });
    await resetFields();
    const res = await overForm(data);
    record.value = (res.flowOver || {}) as M8OrderPicFlowOver;
    record.value.__t = new Date().getTime();
    getTitle.value.title =
      record.value.orderPic.picno +
      '|' +
      record.value.orderPic.picname +
      '|' +
      record.value.oper.operName;
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
    
  });

  async function handleSubmit() {
    await validate();
    openModal(true, '');
  }
  async function handleOk() {
    handSave('1');
  }
  async function closeFunc() {
    handSave('0');
  }

  async function handSave(type) {
   
    try {
      const data = await validate();
      setModalProps2({ confirmLoading: true });
      loading.value = true;
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      data.boverOper = type;
      const res = await overSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', '');
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage('您填写的信息有误，请根据提示修正。');
      }
      console.log('error', error);
    } finally {
      loading.value = false;
      setModalProps2({ confirmLoading: false });
      setDrawerProps({ confirmLoading: false });
      setTimeout(closeModal2);
    }



  }

  const inputFormSchemas: FormSchema[] = [
    {
      label: '工序',
      field: 'oper.operCode',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
      show: false,
    },
    {
      label: '图纸',
      field: 'orderPic.id',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
      show: false,
    },
    {
      label: '流程',
      field: 'orderFlow.id',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
      show: false,
    },
    {
      label: '订单',
      field: 'order.id',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
      show: false,
    },
    {
      label: '完工数',
      field: 'iqty',
      component: 'Input',
      componentProps: {
        maxlength: 10,
      },
      required: true,
    },
    {
      label: '负责人',
      field: 'manCodes',
      fieldLabel: 'manNames',
      component: 'TreeSelect',
      required: true,
      componentProps: {
        api: officeTreeData,
        params: { isLoadUser: true, userIdPrefix: '' },
        canSelectParent: false,
        allowClear: true,
      },
    },
    {
      label: '完工说明',
      field: 'remarks',
      component: 'InputTextArea',
      // required: true,
      componentProps: {
        maxlength: 500,
        rows: 5,
      },
      // colProps: { lg: 24, md: 24 },
    },
  ];
  const [registerForm, { resetFields, setFieldsValue, validate, setProps }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });
</script>
<style scoped>
  .ant-picker {
    width: 80%;
  }
</style>
